/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package chatroom.com.util;

import chatroom.com.entity.UserModel;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

public class UserService {

    public static boolean register(UserModel user) {
        boolean flag = true;
        String sql = "insert into tb_user values(0," + "'" + user.getUserName() + "','"
                + user.getUserPass() + "','" + user.getEmail() + "','" + DateUtil.getCurrentTimestamp() + "','"
                + DateUtil.getCurrentTimestamp() + "','" + user.getCity() + "','"
                + user.getNick() + "','" + user.getSex() + "'," + user.getAge() + ",'" + user.getQq() + "'," + "0)";
        try {
            Statement st = DbManager.getConnection().createStatement();
            flag = st.execute(sql);
        } catch (SQLException ex) {
            Logger.getLogger(UserService.class.getName()).log(Level.SEVERE, null, ex);
        }
        return flag;
    }

    public static boolean judgeLogin(String userName, String userPass) throws SQLException {
        boolean flag = false;
        String sql = "select * from tb_user where userName='" + userName + "' and userPass='" + userPass + "'";
        ResultSet rs = DbManager.getResult(sql);
        while (rs.next()) {
            UserModel user = new UserModel();
            user.setUserName(rs.getString("userName"));
            user.setUserPass(rs.getString("userPass"));
            user.setAge(rs.getInt("age"));
            user.setCity(rs.getString("city"));
            user.setEmail(rs.getString("email"));
            user.setNick(rs.getString("nick"));
            user.setQq(rs.getString("qq"));
            user.setSex(rs.getString("sex"));
            if (user.getUserName() != null && user.getUserPass() != null) {
                flag = true;
            }
        }

        return flag;
    }

    public static UserModel getUserByName(String userName) throws SQLException {
        String sql = "select * from tb_user where userName='" + userName + "'";
        ResultSet rs = DbManager.getResult(sql);
        UserModel user = new UserModel();
        while (rs.next()) {
            user.setUserName(rs.getString("userName"));
            user.setUserPass(rs.getString("userPass"));
            user.setAge(rs.getInt("age"));
            user.setCity(rs.getString("city"));
            user.setEmail(rs.getString("email"));
            user.setNick(rs.getString("nick"));
            user.setQq(rs.getString("qq"));
            user.setSex(rs.getString("sex"));
            user.setGuanliyuan(rs.getInt("guanliyuan"));
        }
        return user;
    }

    public static UserModel getUserByNick(String nick) throws SQLException {
        String sql = "select * from tb_user where nick='" + nick + "'";
        ResultSet rs = DbManager.getResult(sql);
        UserModel user = new UserModel();
        while (rs.next()) {
            user.setUserName(rs.getString("userName"));
            user.setUserPass(rs.getString("userPass"));
            user.setAge(rs.getInt("age"));
            user.setCity(rs.getString("city"));
            user.setEmail(rs.getString("email"));
            user.setNick(rs.getString("nick"));
            user.setQq(rs.getString("qq"));
            user.setSex(rs.getString("sex"));
            user.setGuanliyuan(rs.getInt("guanliyuan"));
        }
        return user;
    }
    
    public static UserModel getUserByNameAndEmail(String name,String email) throws SQLException {
         String sql = "select * from tb_user where userName='" + name + "' and email='"+email+"'";
        ResultSet rs = DbManager.getResult(sql);
        UserModel user = new UserModel();
        while (rs.next()) {
            user.setUserName(rs.getString("userName"));
            user.setUserPass(rs.getString("userPass"));
            user.setAge(rs.getInt("age"));
            user.setCity(rs.getString("city"));
            user.setEmail(rs.getString("email"));
            user.setNick(rs.getString("nick"));
            user.setQq(rs.getString("qq"));
            user.setSex(rs.getString("sex"));
            user.setGuanliyuan(rs.getInt("guanliyuan"));
        }
        return user;
    }
}
